package cn.edu.scau.cmi.tangqingning.controll;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.scau.cmi.tangqingning.dao.StudentDao;
import cn.edu.scau.cmi.tangqingning.dao.impl.StudentDaoImpl;
import cn.edu.scau.cmi.tangqingning.model.Group;
import cn.edu.scau.cmi.tangqingning.model.Student;

/**
 * 控制层 -评价小组的逻辑处理<br>
 * 当客户使用get方式请求Servlet时，Web容器调用doGet方法处理请求，通过调用StudentDao的calculateScore方法计算评分
 * @author 刘岳鑫
 *@see cn.edu.scau.cmi.tangqingning.dao.StudentDao#calculateScore
 */
public class EvaluateGroupServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 重写doGet方法
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @throws ServletException 如果一个错误发生，则抛出此异常
	 * @throws IOException 如果一个错误发生，则抛出此异常
	 */

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/* 先记录学生评价的分数和他/她的学号 */
		HttpSession session = request.getSession();
		Long id = Long.parseLong((String) session.getAttribute("username")); // 获取学生学号，以便记录谁评价了
		int groupNumber = Integer.parseInt(request.getParameter("groupNumber"));
		double functionScore = Double.parseDouble(request.getParameter("functionScore")); // 功能平均分
		double interfaceScore = Double.parseDouble(request.getParameter("interfaceScore")); // 界面效果平均分
		double innovationScore = Double.parseDouble(request.getParameter("innovationScore")); // 创新性平均分
		double oralDefenseScore = Double.parseDouble(request.getParameter("oralDefenseScore")); // 答辩表现平均分
		Student student = new Student(id, functionScore, interfaceScore, innovationScore, oralDefenseScore);
		/* 创建小组对象，把分数存进去，然后存进数据库 */
		Group group = new Group(groupNumber); // 获取被评小组的属性
		StudentDao studentDao = new StudentDaoImpl();
		int[] flagArray = null;
		try {
			flagArray = studentDao.calculateScore(group, student);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (flagArray[0] == 1) {
			request.setAttribute("msg", "<------已评价过此小组------>");
			request.getRequestDispatcher("studentMain.jsp").forward(request, response);
		} else if (flagArray[1] == 1) {
			request.setAttribute("msg", "<------评价成功------>");
			request.getRequestDispatcher("studentMain.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "<------评价失败------>");
			request.getRequestDispatcher("studentMain.jsp").forward(request, response);
		}

	}

}
